﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MvcEx.Configuration;
using Moq;

namespace MvcEx.Tests
{
    [TestFixture]
    public class MvcExControllerBuilderConfigurationFactoryTests
    {
        [Test]
        public void create_configuration_with_assemblies()
        {
            var testConfiguration = new MvcExConfiguration();
            testConfiguration.Id = "something";
            testConfiguration.Modules = new MvcExConfigurationModule[]{
                new MvcExConfigurationModule()
                { 
                    Name = "Test"
                }
            };
            var testModule = new MvcExModule()
            {
                Name = "Some Test",
                Assemblies = new MvcExModuleAssembly[] { new MvcExModuleAssembly() { Name = "MvcEx.Tests" } }
            };

            var applicationMock = new Mock<IMvcExApplication>();
            applicationMock.Setup(x => x.GetActiveConfiguration()).Returns(testConfiguration);
            applicationMock.Setup(x => x.GetModuleById(It.IsAny<String>())).Returns(testModule);

            var configuration = MvcExControllerBuilderConfigurationFactory.CreateBuilderConfigurationFromApplication
                (applicationMock.Object,
                testConfiguration);

            Assert.NotNull(configuration);
            Assert.AreEqual("something", configuration.BuilderIdentifier);
            Assert.AreEqual(1, configuration.Assemblies.Length);
  
        }

        [Test]
        public void create_configuration_with_duplicate_assemblies()
        {
            var testConfiguration = new MvcExConfiguration();
            testConfiguration.Id = "something";
            testConfiguration.Modules = new MvcExConfigurationModule[]{
                new MvcExConfigurationModule()
                { 
                    Name = "Test"
                }
            };
            var testModule = new MvcExModule()
            {
                Name = "Some Test",
                Assemblies = new MvcExModuleAssembly[]
                { 
                    new MvcExModuleAssembly() { Name = "MvcEx.Tests" },
                    new MvcExModuleAssembly() { Name = "MvcEx.Tests" },
                    new MvcExModuleAssembly() { Name = "MvcEx.Tests" }
                }
            };

            var applicationMock = new Mock<IMvcExApplication>();
            applicationMock.Setup(x => x.GetActiveConfiguration()).Returns(testConfiguration);
            applicationMock.Setup(x => x.GetModuleById(It.IsAny<String>())).Returns(testModule);
            
            var configuration = MvcExControllerBuilderConfigurationFactory.CreateBuilderConfigurationFromApplication
                (applicationMock.Object,
                testConfiguration);

            Assert.NotNull(configuration);
            Assert.AreEqual("something", configuration.BuilderIdentifier);
            Assert.AreEqual(1, configuration.Assemblies.Length);


        }
    }
}
